import {messages} from "share/common";
import React from 'react'
import { connect } from 'react-redux'
import { Form, Button, Tag, message } from 'antd'

import ListSearcher from 'components/list-searcher'
import RSService from 'containers/setting/role-setting/role-setting.service'
import 'styles/setting/role-setting/finance-role/finance-role-detail.scss'

class DetailLegalEntity extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      corporationEntity: [],
      showListSearcher: false,
    }
  }

  componentDidMount() {
    this.setState({ corporationEntity: this.props.corporationEntity || [] })
  }

  componentWillReceiveProps(nextProps) {
    this.setState({ corporationEntity: nextProps.corporationEntity || [] })
  }

  //添加法人实体
  handleAddEntity = (values) => {
    let params = {roleOID: this.props.roleOID};
    let corporationEntityOIDs = [];
    values.result.map(item => {
      corporationEntityOIDs.push(item.companyReceiptedOID)
    });
    params.corporationEntityOIDs = corporationEntityOIDs;
    RSService.updateCorporationEntity(params).then(res => {
      this.setState({
        showListSearcher: false,
        corporationEntity: res.data
      });
      message.success(messages('common.save.success', {name: ''}))
    })
  };

  render() {
    const { roleBaseInfo } = this.props;
    const { corporationEntity, showListSearcher } = this.state;
    return (
      <div className="detail-legal-entity">
        {/*系统预设角色不可编辑法人实体*/}
        {roleBaseInfo.roleID !== 'SuperAud' && roleBaseInfo.roleID !== 'SuperFin' && (
          <Button type="primary" className="btn-edit" onClick={() => {this.setState({ showListSearcher: true })}}>{messages('common.edit')}</Button>
        )}
        {(roleBaseInfo.roleID === 'SuperAud' || roleBaseInfo.roleID === 'SuperFin') && (<Tag>{messages('common.all')}</Tag>)}
        {roleBaseInfo.roleID !== 'SuperAud' && roleBaseInfo.roleID !== 'SuperFin' && (
          <div className="tags-chosen">
            {corporationEntity.map(item => {
              return <Tag key={item.companyReceiptedOID}>{item.companyName}</Tag>
            })}
          </div>
        )}
        <ListSearcher visible={showListSearcher}
                      type='corporation_entity'
                      onCancel={() => {this.setState({showListSearcher: false})}}
                      onOk={this.handleAddEntity}
                      selectedData={corporationEntity}
                      labelKey='companyName'
                      showDetail={true}
                      externalLabel={item => item.enable ? '' : <span style={{color: '#999'}}>（{messages('common.disabling')}）</span>}
                      searchPlaceHolder={messages('setting.key906'/*请输入关键字*/)}/>
      </div>
    )
  }
}

DetailLegalEntity.propTypes = {
  corporationEntity: React.PropTypes.array,
  roleBaseInfo: React.PropTypes.object,
  roleOID: React.PropTypes.string,
};

DetailLegalEntity.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps(state) {
  return {
    profile: state.login.profile,
  }
}

const wrappedDetailLegalEntity = Form.create()(DetailLegalEntity);

export default connect(mapStateToProps)(wrappedDetailLegalEntity)
